﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NOMFinalSettlement.CrystalReport;
using NOMFinalSettlement.DatasetReport;
using NHTU;
using CrystalDecisions.Shared;

namespace NOMFinalSettlement.AppForm.Program
{
    public partial class fReportAll : DevComponents.DotNetBar.Office2007Form
    {
        FinalSettlementModel.TableHoSo profile = null;
        FinalSettlementModel.TableCanBo officer = null;

        string formatMoney = "{0:0,0.00}";

        public fReportAll()
        {
            InitializeComponent();
        }

        private void fReportAll_Load(object sender, EventArgs e)
        {
            profile = new FinalSettlementModel.TableHoSo();
            officer = new FinalSettlementModel.TableCanBo();
        }

        private void btnProcessing_Click(object sender, EventArgs e)
        {
            if (bgw.IsBusy != true)
            {
                bgw.RunWorkerAsync();
            }

        }

        void doSearch()
        {
            dsReportAll reportAllds = new dsReportAll();
            var tblProfile = profile.listProfile();
            var tblOfficer = officer.listOfficer();
            if (dateEnd.Value < dateStart.Value)
            {
                MsgBox.Show("Điều kiện lọc không đúng. Vui lòng nhập lại!", "Thông Báo", MsgBoxButton.YesNo, MsgBoxIcon.Stop);
                return;
            }
            if (dateStart.Text != "")
            {
                tblProfile = tblProfile.Where(w => w.NgayNhan >= dateStart.Value);
            }
            if (dateEnd.Text != "")
            {
                tblProfile = tblProfile.Where(w => w.NgayNhan <= dateEnd.Value);
            }
            int stt = 1;
            foreach (var rowOfficer in tblOfficer)
            {
                var tblProfile_Officer = tblProfile.Where(w => w.CanBo == rowOfficer.CanBo);
                
                DataRow row = reportAllds.dtReportAll.NewRow();
                row["colSTT"] = stt;
                row["colCanBoThamTra"] = rowOfficer.CanBo;
                row["colHoSoDaNhan"] = tblProfile_Officer.Count();
                if (rowOfficer.CanBo != "Chưa Giao" && tblProfile_Officer.Count() > 0)
                {
                    row["colHoSoHoanThanh"] = tblProfile_Officer.Count(c => c.HoanThanhQuyetToan == "Đã hoàn thành");
                    row["colTongGiaTriQT"] = String.Format(formatMoney, tblProfile_Officer.Sum(s => s.GiaTriQuyetToan) / 1000);
                    row["colTangGiam"] = String.Format(formatMoney, (tblProfile_Officer.Sum(s => s.GiaTriQuyetToan) - tblProfile_Officer.Sum(s => s.DeNghiGiaTriQuyetToan)) / 1000);
                    row["colBinhQuan"] = String.Format(formatMoney, (tblProfile_Officer.Sum(s => s.GiaTriQuyetToan) / tblProfile_Officer.Count()) / 1000);
                }
                else
                {
                    row["colHoSoHoanThanh"] = "0";
                    row["colTongGiaTriQT"] = "0";
                    row["colTangGiam"] = "0";
                    row["colBinhQuan"] = "0";
                }
                if (tblProfile.Count() > 0)
                {
                    row["totalHoSoDaNhan"] = tblProfile.Count();
                    row["totalHoSoHoanThanhQT"] = tblProfile.Count(c => c.HoanThanhQuyetToan == "Đã hoàn thành");
                    if (dateStart.Text != "")
                    {
                        row["totalHoSoNhanTruoc"] = tblProfile.Count(c => c.NgayNhan < dateStart.Value);
                    }
                    else row["totalHoSoNhanTruoc"] = 0;
                    row["totalDeNghiQuyetToan"] = String.Format(formatMoney, tblProfile.Sum(s => s.DeNghiGiaTriQuyetToan));
                    row["totalGiaTriQuyetToan"] = String.Format(formatMoney, tblProfile.Sum(s => s.GiaTriQuyetToan));
                    row["totalTangGiam"] = String.Format(formatMoney, tblProfile.Sum(s => s.GiaTriQuyetToan) - tblProfile.Sum(s => s.DeNghiGiaTriQuyetToan));
                    row["totalBinhQuanTrenCongTrinh"] = String.Format(formatMoney, tblProfile.Sum(s => s.GiaTriQuyetToan) / tblProfile.Count());
                }
                reportAllds.dtReportAll.Rows.Add(row);
                stt++;
            }
            cReportAll reportAllcr = new cReportAll();            
            reportAllcr.SetDataSource(reportAllds);
            Invoke(new MethodInvoker(() => crystalReportViewer1.ReportSource = reportAllcr), null);
            reportAllcr.SetParameterValue("dateFrom", dateStart.Text);
            reportAllcr.SetParameterValue("dateTo", dateEnd.Text);
            //crystalReportViewer1.Refresh();            
        }

        private void timerLoading_Tick(object sender, EventArgs e)
        {
            circularProgress.Value += 2;
        }

        void EnableCircular(bool enable)
        {
            circularProgress.Visible = enable;
            circularProgress.IsRunning = enable;
            if (enable)
            {
                timerLoading.Start();
            }
            else
            {
                timerLoading.Stop();
            }
        }

        private void bgw_DoWork(object sender, DoWorkEventArgs e)
        {
            EnableCircular(true);
            btnProcessing.Enabled = false;
            lbStatus.Text = " Đang xử lý ...";
            doSearch();
            
        }

        private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            
        }

        private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            EnableCircular(false);
            btnProcessing.Enabled = true;
            lbStatus.Text = "";
        }

    }
}
